package com.zxy.zxyrpc.server;

import io.vertx.core.Vertx;

/**
 * @author zxy
 * @since 2025/5/11
 **/
public class VertHttpServerImpl implements HttpServer {
    @Override
    public void doStart(int port) {
        // vertx 对象实例
        Vertx vertx = Vertx.vertx();
        // Http 服务器对象实例
        io.vertx.core.http.HttpServer httpServer = vertx.createHttpServer();

        // 请求响应处理
        /*
        // 简单实现
        httpServer.requestHandler(request -> {
            System.out.println("接收到请求：" + request.method() + " " + request.uri());

            request.response()
                    .putHeader("content-type", "text-plain")
                    .end("Hello from vert.x Http Server");
        });*/

        httpServer.requestHandler(new HttpServerHandler());
        // 开启监听
        httpServer.listen(port, result -> {
            if (result.succeeded())
                System.out.println("服务现在监听的端口号：" + port);
            else
                System.err.println("服务启动失败: " + result.cause());
        });

    }
}
